<%--
  Created by IntelliJ IDEA.
  User: houxianghua
  Date: 2015/7/24
  Time: 8:59
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title></title>
    <%@include file="/WEB-INF/jsp/component/commonHead.jsp" %>
    <%@include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
    <style>
        .text-center{
            text-align:center;
        }

    </style>
</head>
<%--<body onresize="reSizeParentIframe()" onload="reSizeParentIframe();">--%>
<body>
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">经销商管理</h1>
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                所属地区:
                <select id="largeArea" style="width: 100px;height: 24px;">
                    <option value="">大区</option>
                    <c:forEach items="${largeAreaList}" var="largeArea">
                        <option value="${largeArea.largeAreaCode}">${largeArea.largeAreaName}</option>
                    </c:forEach>
                </select>
                <select id="provinceArea" style="width: 100px;height: 24px;">
                    <option value="">省</option>
                </select>
                <select id="cityArea" style="width: 100px;height: 24px;">
                    <option value="">市</option>
                </select>
                经销商等级:
                <select id="level" name="level" style="width: 100px;height: 24px;">
                    <option value="" >请选择...</option>
                    <option value="A" >A</option>
                    <option value="B" >B</option>
                    <option value="C" >C</option>
                </select>
                经销商名称:<input type="text" id="companyName" name="companyName" size="18"/>
                <button id="query" class="mk_butt" >查询</button>
                <%--<button id="query" type="button" class="btn btn-outline btn-success">查询</button>--%>
            </div>
            <!-- /.panel-heading -->
            <div class="panel-body">
                <div class="dataTable_wrapper">
                    <table id="example" class="table table-striped table-bordered table-hover" width="100%">
                        <thead>
                        <tr align="left">
                            <th><input id="all_id" type="checkbox"></th>
                            <th>经销商名称</th>
                            <th>经销商等级</th>
                            <th>地址</th>
                            <th>联系电话</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>

<script>

    var showLagreArea=false;

    $(function () {
        $("#largeArea").change(function () {
            if (this.value == null || this.value == "") {
                resetArea(0);
            } else {
                getLowerAreaList("provinceArea", this.value, 0);
                resetArea(1);
            }
        });
        $("#provinceArea").change(function () {
            if (this.value == null || this.value == "") {
                resetArea(1);
            } else {
                getLowerAreaList("cityArea", this.value, 1);
                resetArea(2);
            }
        });
    });
    /**
     * 重置下级菜单
     * */
    function resetArea(level){
        if(level<1){
            var provinceArea = document.getElementById("provinceArea");
            provinceArea.options.length = 1;
        }
        if(level<2){
            var cityArea = document.getElementById("cityArea");
            cityArea.options.length = 1;
        }
//            if(level<3){
//                var countyArea = document.getElementById("countyArea");
//                countyArea.options.length = 1;
//            }
//            if(level<4){
//                var townArea = document.getElementById("townArea");
//                townArea.options.length = 1;
//            }
//            if(level<5){
//                var villageArea = document.getElementById("villageArea");
//                villageArea.options.length = 1;
//            }
    }
    /**
     * 后台获取下级列表
     * */
    function getLowerAreaList(id,code,level){
        $.ajax({
            url:'${ctx}/area/getLowerAreaList',
            type:'post',
            data:'code='+code+"&level="+level,
            async : false, //默认为true 异步
            error:function(){
                alert('error');
            },
            success:function(data){
                var objs = document.getElementById(id);
                objs.options.length = 1;
                var area = data;
                for(var i=0;i<area.length;i++){
                    var childOption = document.createElement("option");
                    childOption.value = area[i].code;
                    childOption.innerHTML = area[i].name;
                    objs.appendChild(childOption);
                }
            }
        });
    }

    var table;
    function initTable() {
        table = $("#example").DataTable({
            "dom": "<'row'<'col-xs-2'l><'#mytool.col-xs-10'><'col-xs-6'f>r>" +
            "t" +
            "<'row'<'col-xs-6'i><'col-xs-6'p>>",
            "bFilter": false,//去掉搜索框
            "bAutoWidth": true, //自适应宽度
            "sPaginationType" : "full_numbers",
//                "bDestroy" : true,
//              "asStripeClasses": [ 'strip1', 'strip2', 'strip3' ],//设置斑马条（奇偶行）的css class
            "ordering": false,//是否允许Datatables开启排序
            "bProcessing" : true,
            "bServerSide" : true,
            "sAjaxSource" : "${URL_ADM_AGENTCOMPANY_SEARCH}",
            "sAjaxDataProp": "dataList",//查询后，返回的集合
            "fnServerData": function ( sSource, aoData, fnCallback ) {  //查询条件
                aoData.push(
                        { "name": "companyName", "value": $("#companyName").val() }
                );
                aoData.push(//level
                        { "name": "areaCode", "value": getAreaCode() }
                );
                aoData.push(
                        { "name": "level", "value": $("#level").val() }
                );
                aoData.push(
                        { "name": "showLagreAreaDec", "value": showLagreArea }
                );
                $.ajax( {
                    "dataType": 'json',
                    "url": sSource,
                    "type":'post',
                    "data": aoData,
                    "success": fnCallback
                } );

            },
            "aoColumns" : [ {
                "targets": -1,
                "data": null,
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html('' +
                            '&nbsp;&nbsp;<input name="id" value="'+oData.id+'" type="checkbox" onClick="isCheckedAll()">' +
                            '');
                }
            }, {
                "mDataProp" : "companyName"
            }, {
                "mDataProp" : "level"
            }, {
                "mDataProp" : "companyAddress"
            }, {
                "mDataProp" : "linkPhine"
            }
            ],
            "oLanguage" : {
                "sProcessing" : "正在加载中......",
                "sLengthMenu" : "每页显示 _MENU_ 条记录",
                "sZeroRecords" : "没有数据！",
                "sEmptyTable" : "无数据存在！",
                "sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                "sInfoEmpty" : "显示0到0条记录",
                "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
                //"sSearch" : "搜索",
                "oPaginate" : {
                    "sFirst" : "首页",
                    "sPrevious" : "上一页",
                    "sNext" : "下一页",
                    "sLast" : "末页"
                }
            },
            initComplete: function () {
                <shiro:hasPermission name="+agentCompany+8">
                $("#mytool").append('<button id="delete_butt" type="button" class="btn btn-outline btn-success" style="float:right;">删除</button>');
                $("#delete_butt").on("click", toDelete);
                </shiro:hasPermission>
                <shiro:hasPermission name="+agentCompany+4">
                $("#mytool").append('<button id="update_butt" type="button" class="btn btn-outline btn-success" style="float:right;">修改</button>');
                $("#update_butt").on("click", toUpdate);
                </shiro:hasPermission>
                <shiro:hasPermission name="+agentCompany+2">
                $("#mytool").append('<button id="add_butt" type="button" class="btn btn-outline btn-success" style="float:right;">新增</button>');
                $("#add_butt").on("click", toAdd);
                </shiro:hasPermission>
                <shiro:hasPermission name="+agentCompany">
                $("#mytool").append('<button id="export_butt" type="button" class="btn btn-outline btn-success" style="float:right;">导出</button>');
                $("#export_butt").on("click", toExport);
                </shiro:hasPermission>
                <shiro:hasPermission name="+agentCompany+2">
                $("#mytool").append('<button id="import_butt" type="button" class="btn btn-outline btn-success" style="float:right;">导入</button>');
                $("#import_butt").on("click", toImport);
                </shiro:hasPermission>
            }
        });
    }

    function getAreaCode(){
        showLagreArea=false;
        if($("#cityArea").val()!=""){
            return $("#cityArea").val();
        }
        if($("#provinceArea").val()!=""){
            return $("#provinceArea").val();
        }
        if($("#largeArea").val()!=""){
            showLagreArea=true;
            return $("#largeArea").val();
        }
        return "";
    }

    //新增
    function toAdd(){
        window.location.href='${URL_ADM_AGENTCOMPANY_TOADD}';
    }

    //修改
    function toUpdate(){
        var checkedArray = getCheckedArray();
        if(checkedArray.length!=1){
            top.bootbox.dialog({
                message: "请选中一项数据",
                buttons: {
                    OK: {
                        label: "确定",
                        className: "btn-primary"
                    }
                }
            });
            return false;
        }
        window.location.href='${URL_ADM_AGENTCOMPANY_TOUPDATE}?id=' +checkedArray[0];
    }

    //删除
    function toDelete(){
        var checkedArray = getCheckedArray();
        var ids = "";
        if(checkedArray.length==0){
            top.bootbox.dialog({
                message: "请选中至少一项数据",
                buttons: {
                    OK: {
                        label: "确定",
                        className: "btn-primary"
                    }
                }
            });
            return false;
        }else{
            for(var i=0; i<checkedArray.length;i++){
                ids += checkedArray[i]+",";
            }
            ids = ids.substring(0,ids.length-1);
        }
        top.bootbox.dialog({
            message: "确定删除此项？",
            buttons: {
                OK: {
                    label: "确定",
                    className: "btn-primary",
                    callback: function () {
                        $.ajax({
                            url:'${URL_ADM_AGENTCOMPANY_DELETES}',//删除地址
                            type:'post',
                            data:{ids:ids},
                            async : false, //默认为true 异步
                            error:function(){
                                alert('error');
                            },
                            success:function(data){
                                refreshTable();
                            }
                        });
                    }
                },
                Cancel: {
                    label: "取消",
                    className: "btn-default"
                }
            }
        });
    }

    //多选按钮
    $("#all_id").click(function(){
        if(this.checked){
            $("input[name='id']").each(function(){
                $(this)[0].checked = true;
            });
        }else{
            $("input[name='id']").each(function(){
                $(this)[0].checked = false;
            });
        }
    });

    //判断是否已经全选
    function isCheckedAll(){
        var isCheckAll = true;
        $("input[name='id']").each(function(){
            if(!this.checked){
                isCheckAll = false;
            }
        });
        if(isCheckAll){
            $("#all_id")[0].checked = true;
        }else{
            $("#all_id")[0].checked = false;
        }
    }

    function getCheckedArray(){
        var checkedArray = [];
        $("input[name='id']").each(function(){
            if(this.checked){
                checkedArray.push(this.value);
            }
        });
        return checkedArray;
    }

    //新增提交  调用子级页面新增方法
    $("#submit_add").click(function(){
        document.getElementById('addIframe').contentWindow.doAdd();
    });

    //修改提交  调用子级页面修改方法
    $("#submit_update").click(function(){
        document.getElementById('updateIframe').contentWindow.doUpdate();
    });

    //关闭弹出框
    function closeModal(flag){
        $('#'+flag+"Modal").modal('hide');
        refreshTable();
    }

    //查询
    $("#query").click(function() {
        refreshTable();
    });

    //导入
    function toImport() {
        window.parent.showModelIframe('${URL_ADM_AGENTCOMPANY_TOIMPORT}',"导入Excel",800,400);
//        $("#importModal").modal({ show: true, backdrop: 'static' });
    }

    //导出
    function toExport() {
        window.parent.showModelIframe('${URL_ADM_AGENTCOMPANY_TOEXPORT}',"导入Excel",800,400);
        <%--location.href = '${URL_ADM_AGENTCOMPANY_EXPORT}?companyName='+$("#companyName").val()+"&areaCode="+getAreaCode()+"&level="+$("#level").val()+"&showLagreAreaDec="+showLagreArea;--%>
    }

    //刷新table数据
    function refreshTable(){
        table.ajax.reload();
        //重置全选按钮
        $("#all_id")[0].checked = false;
    }

    $(document).ready(function() {
        initTable();
    });
</script>
</body>
</html>